package controleImpl;

import interfaceGrafica.GUI;

public class ElevatorController {

	private ElevatorStatusPlan statusPlanE1 = null;
	private ElevatorStatusPlan statusPlanE2 = null;

	private MotorImpl motorE1 = null;
	private MotorImpl motorE2 = null;

	private ArrivalSensorImpl asE1 = null;
	private ArrivalSensorImpl asE2 = null;

	private Motion mE1 = null;
	private Motion mE2 = null;

	private Scheduler s = null;

	public ElevatorStatusPlan getStatusPlanE1() {
		return statusPlanE1;
	}

	public ElevatorStatusPlan getStatusPlanE2() {
		return statusPlanE2;
	}

	public MotorImpl getMotorE1() {
		return motorE1;
	}

	public MotorImpl getMotorE2() {
		return motorE2;
	}

	public ArrivalSensorImpl getAsE1() {
		return asE1;
	}

	public ArrivalSensorImpl getAsE2() {
		return asE2;
	}

	public Motion getmE1() {
		return mE1;
	}

	public Motion getmE2() {
		return mE2;
	}

	private Thread tE1 = null;
	private Thread tE2 = null;

	private static ElevatorController ec = null;

	public ElevatorController(){

		statusPlanE1 = new ElevatorStatusPlan();
		statusPlanE2 = new ElevatorStatusPlan();

		motorE1 = new MotorImpl(1);
		motorE2 = new MotorImpl(2);

		asE1 = new ArrivalSensorImpl(1);
		Thread tasE1 = new Thread(asE1);
		tasE1.start();
		asE2 = new ArrivalSensorImpl(2);
		Thread tasE2 = new Thread(asE2);
		tasE2.start();

		mE1 = new Motion(1);
		mE2 = new Motion(2);

		tE1 = new Thread(mE1);
		tE1.start();
		tE2 = new Thread(mE2);
		tE2.start();

		s = new Scheduler();
	}

	public boolean requestElevator(int floor, boolean up){
		if (asE1.getCurrentFloor() != floor && asE2.getCurrentFloor() != floor){
			if (s.selectElevator(floor, up) == 1){
				statusPlanE1.update(1, floor, up);
				return true;
			}
			else{
				statusPlanE2.update(2, floor, up);
				return true;				
			}
		}
		return false;
	}

	public boolean selectDirection(int elevator, int floor){

		if (elevator == 1){
			if (asE1.getCurrentFloor() > floor){//Desce

				return false;
			}
			else{//Sobe

				return true;
			}
		}
		else{
			if (asE2.getCurrentFloor() > floor){//Desce

				return false;
			}
			else{//Sobe

				return true;
			}
		}
	}
	public void selectDestination(int elevator, int floor){

		if (elevator == 1) {
			statusPlanE1.update(elevator, floor, selectDirection(elevator, floor));	
		}
		else{
			statusPlanE2.update(elevator, floor, selectDirection(elevator, floor));
		}
	}

	public boolean checkElevatorFloor(int floor){

		if (asE1.getCurrentFloor() == floor){

			return true;
		}
		else if (asE2.getCurrentFloor() == floor){

			return true;
		}
		else{
			return false;
		}
	}

	public static ElevatorController v(){

		if (ec == null){

			ec = new ElevatorController();
		}
		return ec;
	}

	public static void main(String[] args) {

		GUI.v();
		ec = ElevatorController.v();

		ec.asE1.setCurrentFloor(1, 0);
		ec.asE2.setCurrentFloor(2, 0);
	}
}
